<template>
  <div class="pagination" style="margin: 10px">
    <el-pagination
      @size-change="sizeChange"
      @current-change="currentChange"
      :current-page="currentPage"
      :page-size="pageSize"
      :page-sizes="pageSizes"
      :layout="layoutStyle"
      :total="totalNum"
      :class="isNeedPosition && (totalNum < 10 || totalNum == 0) ? '' : ''"
      :background="backgroundStyle"
    ></el-pagination>
    <!-- bottomPage -->
  </div>
</template>

<script>
/**
 * 分页组件
 * props的数据含义：
 * {number} page - 当前页数
 * {number} limit - 一页显示条数.默认显示5条
 * {number} totalNum - 总条目数
 * {object[]} [pageSizes = [5, 10, 20, 30, 40, 50, 100, 200]] - 每页显示个数选择器的选项设置
 * {string} [layoutStyle = "total, sizes, prev, pager, next, jumper"] - 分页风格布局
 * {boolean} [backgroundStyle = false] - 是否为分页按钮添加背景色
 */
export default {
  props: {
    page: {
      type: Number,
      default: 1,
    },
    limit: {
      type: Number,
      default: 5,
    },
    totalNum: {
      type: Number,
      default: 0,
    },
    isNeedPosition: {
      type: Boolean,
      default: true,
    },
    pageSizes: {
      type: Array,
      default: () => [2, 3, 5, 10, 20, 30, 40, 50, 100, 200],
    },
    layoutStyle: {
      type: String,
      default: "total, sizes, prev, pager, next, jumper",
    },
    backgroundStyle: {
      type: Boolean,
      default: false,
    },
    // 为了区分字典页面的父级和自己页面点击事件
    pageParent: {
      type: Boolean,
      default: true,
    },
  },

  data() {
    return {};
  },
  computed: {
    currentPage: {
      get() {
        return this.page;
      },
      set(val) {
        this.$emit("update:page", val);
      },
    },
    pageSize: {
      get() {
        return this.limit;
      },

      set(val) {
        this.$emit("update:limit", val);
      },
    },
  },
  methods: {
    //每页条数pageSize改变，比如一页显示10条变成一页显示20条
    sizeChange(val) {
      this.limit  = val
      this.$emit(
        "pagechange",
        { page: this.page, limit: val },
        this.pageParent
      );
      // this.$emit("limitchange", { page: this.page, limit: val }); // 别删除
    },

    //当前页数currentPage改变,比如从第一页变成第二页
    currentChange(val) {
      this.page = val
      this.$emit(
        "pagechange",
        { page: val, limit: this.limit },
        this.pageParent
      );
    },
  },
};
</script>
<style lang="stylus" scoped></style>
